package com.youtui.manager.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.youtui.common.constant.ResultMessage;
import com.youtui.common.constant.SystemConstant;
import com.youtui.common.util.ResultUtil;
import com.youtui.manager.entity.AccountInfoEntity;
import com.youtui.manager.model.AccountInfo;
import com.youtui.manager.model.Developer;
import com.youtui.manager.service.IAccountInfoService;
import com.youtui.manager.service.IDeveloperService;
import com.youtui.manager.service.ISmsCodeService;

@Controller
@RequestMapping("/accountInfo")
public class AccountInfoController extends BaseController {

	@Autowired
	private IAccountInfoService accountInfoService;

	@Autowired
	private IDeveloperService developerService;

	@Autowired
	private ISmsCodeService smsCodeService;

	@RequestMapping("/load.do")
	@ResponseBody
	public ResultMessage load(Long devId) {

		if (devId == null) {
			return ResultUtil.result(ResultUtil.code_fail, "参数错误");
		}

		AccountInfo accountInfo = accountInfoService.findAccountInfoByDevId(devId);
		AccountInfoEntity acctInfo = new AccountInfoEntity();
		BeanUtils.copyProperties(accountInfo, acctInfo);

		Developer dev = developerService.findDeveloperById(accountInfo.getDevId());
		acctInfo.setMobile(dev.getMobile());

		return ResultUtil.result(ResultUtil.code_success, "账户信息", acctInfo);
	}

	/**
	 * 更新账户基本信息
	 * 
	 * @param accountInfo
	 * @return
	 */
	@RequestMapping("/update.do")
	@ResponseBody
	public ResultMessage updateAccount(AccountInfo accountInfo, String smsCode) {

		if (accountInfo.getId() == null) {
			return ResultUtil.result(ResultUtil.code_fail, "参数错误");
		}

		if (StringUtils.isBlank(smsCode)) {
			return ResultUtil.result(ResultUtil.code_fail, "短信验证码错误");
		}

		Developer dev = developerService.findDeveloperById(accountInfo.getDevId());

		boolean flag = smsCodeService.checkSmsCode(dev.getMobile(), smsCode, SystemConstant.SmsCode_Type_login);

		if (!flag) {
			return ResultUtil.result(ResultUtil.code_fail, "短信验证码错误");
		}

		AccountInfo accti = accountInfoService.findAccountInfoByDevId(accountInfo.getDevId());

		if (accountInfo.getAccountType() == null) {
			return ResultUtil.result(ResultUtil.code_fail, "财务类型不能为空");
		}
		if (accti.getAccountType()!=null && accti.getAccountType() != accountInfo.getAccountType()) {
			return ResultUtil.result(ResultUtil.code_fail, "财务类型不能修改");
		}

		if (StringUtils.isBlank(accountInfo.getAccountName()) || StringUtils.isBlank(accountInfo.getAccountCardPathBack())
				|| StringUtils.isBlank(accountInfo.getAccountCardPathFront())
				|| StringUtils.isBlank(accountInfo.getAccountCard())
				|| StringUtils.isBlank(accountInfo.getBankName())
				|| StringUtils.isBlank(accountInfo.getBankNo())
				|| StringUtils.isBlank(accountInfo.getBankSubName())
				|| StringUtils.isBlank(accountInfo.getBankUserName())) {
			return ResultUtil.result(ResultUtil.code_fail, "提交信息不完整");
		} 

		accountInfoService.update(accountInfo);
		return ResultUtil.result(ResultUtil.code_success, "账户信息修改成功");
	}

}
